#!/bin/bash

ROOT_UID=0
DEST_DIR=

# Destination directory
if [ "$UID" -eq "$ROOT_UID" ]; then
  DEST_DIR="/usr/share/icons"
else
  DEST_DIR="$HOME/.local/share/icons"
fi

SRC_DIR=$(cd $(dirname $0) && pwd)

show_question() {
  echo -e "\033[1;34m$@\033[0m"
}

show_tips() {
  echo -e "\033[1;32m$@\033[0m"
}

show_dir() {
  echo -e "\033[1;32m$@\033[0m"
}

show_error() {
  echo -e "\033[1;31m$@\033[0m"
}

end() {
  echo -e "\nExiting...\n"
  exit 0
}

continue() {
  show_question "\nDo you want to continue? (y)es, (n)o : \n"
  read INPUT
  case $INPUT in
    [Yy]* ) ;;
    [Nn]* ) end;;
    * ) show_error "\nSorry, try again."; continue;;
  esac
}

radius() {
  show_question "\nWhich style of headerbar button do you prefer?: (f)circle, (g)square : \n"
  read INPUT
  case $INPUT in
    [Ff]* ) radius="standard" && install;;
    [Gg]* ) radius="square" && install;;
    * ) show_error "\nSorry, try again."; continue;;
  esac
}

device() {
  show_question "\nWhat is your device? Laptop or PC (d)laptop, (e)pc : \n"
  read INPUT
  case $INPUT in
    [Dd]* ) size="compact" && radius;;
    [Ee]* ) size="standard" && radius;;
    * ) show_error "\nSorry, try again."; continue;;
  esac
}

main() {
  show_question "\nWhat color variant do you want to install: (a)standard, (b)light, (c)dark : \n"
  read INPUT
  case $INPUT in
    [Aa]* ) color="standard" && device;;
    [Bb]* ) color="light" && device;;
    [Cc]* ) color="dark" && device;;
    * ) show_error "\nSorry, try again."; continue;;
  esac
}

setup() {
  show_question "\nDo you want to set the theme now? (y)es, (n)o : \n"
  read INPUT
  case $INPUT in
    [Yy]* ) Canta_icon;;
    [Nn]* ) end;;
    * ) show_error "\nSorry, try again."; continue;;
  esac
}

Canta_icon() {

# Set Canta Icon Theme
  echo "Setting Canta..."
  gsettings set org.gnome.desktop.interface icon-theme Canta
  echo "Done!"

}

install_icon() {

  # Show destination directory
  echo -e "\nCanta icon theme will be installed in:\n"
  show_dir "\t$DEST_DIR"
  if [ "$UID" -eq "$ROOT_UID" ]; then
    echo -e "\nIt will be available to all users."
  else
    echo -e "\nIf you want to make them available to all users, run this script as root."
  fi

  # INSTALL

  echo -e "\nInstalling Canta icon theme..."

  # Copying files
  cp -ur ${SRC_DIR}/src/icons/Canta ${DEST_DIR}

  # update icon caches
  gtk-update-icon-cache ${DEST_DIR}/Canta

  echo -e "\nInstallation complete!"
  show_tips "\nIf you want a better experience you should install numix-circle first!"
  show_tips "Because Canta icon theme use numix-icon-theme-circle icon theme for Inherits!\n"

  setup
}

icon_question() {
  show_question "\nDo you want to install Canta icon theme?: (y)es, (n)o : \n"
  read INPUT
  case $INPUT in
    [Yy]* ) install_icon;;
    [Nn]* ) end;;
    * ) show_error "\nSorry, try again."; continue;;
  esac
}

install() {
  ./install.sh --color ${color} --size ${size} --radius ${radius}
  icon_question
}

echo -e "\e[1m\n+------------------------------------------+"
echo -e "|       Canta Theme Installer Script       |"
echo -e "+------------------------------------------+\n\e[0m"

main

